/*
 * @lc app=leetcode.cn id=209 lang=swift
 *
 * [209] 长度最小的子数组
 */

// @lc code=start
class Solution {
    func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int {
        var left = 0
        var sum = 0
        var result = nums.count + 1
        for right in 0 ..< nums.count {
            sum += nums[right]
            while sum >= target {
                result = min(right - left + 1, result)
                sum -= nums[left]
                left += 1
            }
        }
        return result > nums.count ? 0 : result
    }
}
// @lc code=end

